package mapper.impl;

import entity.Student;
import mapper.IStudentMapper;
import util工具包.JDBCutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentMapperImpl implements IStudentMapper {
    @Override//添加
    public  boolean addStu(String no, String name, String account, String sex, String phone, String age) {
        Connection conn = JDBCutil.getconnection();
        int rs;
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement("INSERT INTO `t_student`(no,name,account,sex,phone,age) VALUES(?,?,?,?,?,?);");
            pst.setString(1,no);
            pst.setString(2,name);
            pst.setString(3,account);
            pst.setString(4,sex);
            pst.setString(5,phone);
            pst.setString(6,age);
            rs = pst.executeUpdate();
            if (rs == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(conn,pst,null);
        }
        return false;
    }

    @Override//修改
    public boolean updateStu(String no, String name, String account, String sex, String phone, String age) {
        Connection conn = JDBCutil.getconnection();
        int rs;
        PreparedStatement pst = null;
        try {
          pst=conn.prepareStatement("UPDATE t_student SET name=?,account=?,sex=?,phone=?,age=? WHERE no=?;");
          pst.setString(1,name);
          pst.setString(2,account);
          pst.setString(3,sex);
          pst.setString(4,phone);
          pst.setString(5,age);
          pst.setString(6,no);


          //首先你这里弄了10个占位符，
            //No value specified for parameter 6 这个意思你应该看到了，你有多少占位符都需要赋值，懂吗？
            //3：mysql语句有问题 这才是修改，where后面只需要加一个唯一的字段就行

            rs = pst.executeUpdate();
            if (rs == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(conn,pst,null);
        }
        return false;
    }

    @Override//删除
    public boolean DelStu(String no, String name) {
        Connection conn = JDBCutil.getconnection();
        PreparedStatement pst = null;
        int rs;
        try {
            pst = conn.prepareStatement("DELETE FROM t_student WHERE no=?;");
            pst.setString(1, no);
            rs = pst.executeUpdate();
            if (rs == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCutil.close(conn, pst, null);
        }
        return false;
    }

    @Override//查
    public List<Student> SelectStu() {
        Connection conn = JDBCutil.getconnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
//            预执行
            pst= conn.prepareStatement("select * from t_student");
//             执行语句
            rs = pst.executeQuery();

//          创建一个集合
            ArrayList<Student> Stulist = new ArrayList<>();

            while (rs.next()){
//               创建一个对象
                Student student = new Student();

                student.setNo(rs.getString("no"));
                student.setName(rs.getString ("name"));
                student.setAccount(rs.getString("account"));
                student.setSex(rs.getString ("sex"));
                student.setPhone(rs.getString ("phone"));
                student.setAge(rs.getString ("age"));
//            把学生对象添加到集合中
                Stulist.add(student);
            }
            return  Stulist;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCutil.close(conn,pst,rs);

        }
        return null;
    }
}
